<?php
class ProfilesController extends AppController {

	var $name = 'Profiles';
	var $helpers = array('Html', 'Form');
	var $uses = array('Profile','Visited');


	function view($id = null) {
		$user = $this->Auth->user();
		$user_id = $user['User']['id'];
		
		if(!$id) {
			$profile = $this->Profile->findByUserId($user_id);
			$id = $profile['Profile']['id'];
		}		
		
		$this->Profile->recursive = 2;

		$this->Profile->City->unbindModel(array('hasMany' => array('Story','Profile')));
		$this->Profile->User->unbindModel(array('hasMany' => array('Story')));
		$this->Profile->Image->unbindModel(array('belongsTo' => array('Profile','Story')));
		$this->Profile->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));
		$profile = $this->Profile->findById($id);
		
		if(!isset($profile['Profile']['id']))
		 $this->cakeError('error404');
		 
		//debug($profile);
		
		$this->set('profile', $profile);
		$this->set('isMe', ($profile['User']['id'] == $user_id));
		$this->set('myUserId', $user_id);
		
		$friends = $this->Profile->User->findFriends($profile['User']['id']);
		//debug($friends);
		//debug($profile['User']['id']);
		
		$isFriend = false;
		foreach ($friends as $friend)
		{
			//debug($friend);		
			if ($friend['Profile']['user_id'] == $user_id && $profile['User']['id'] != $user_id)
			{
				$isFriend = true;
			}
		}
		
		shuffle($friends);
		array_splice($friends,20);
		
		//$test = $this->Profile->User->Friend->find(array('OR' => array('Friend.user_id' => $user_id)));
		//debug($profile['Profile']['date_of_birth']);
		
		//debug($profile['Profile']['date_of_birth'] == '1996-01-00');
		
		$this->set('notsetdob',$profile['Profile']['date_of_birth'] == '0000-00-00');
		$this->set('isFriend',$isFriend);
		$this->set('friends',$friends);
		
		$this->Profile->Post->recursive = 3;
		$this->Profile->Post->unbindModel(array('belongsTo' => array('City','Profile', 'Story')));
		$this->Profile->Post->User->unbindModel(array('hasMany' => array('Story')));
		$this->Profile->Post->User->Profile->unbindModel(array('hasMany' => array('Post'), 'belongsTo' => array('User')));
		$this->Profile->Post->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));
		$posts = $this->Profile->Post->find('all', array('conditions' => array('Post.profile_id' => $id), 'order' => 'Post.created DESC'));
		
		$this->set('posts',$posts);
		//debug($profile);
		
		$this->Profile->User->Story->unbindModel(array('belongsTo' => array('User')));
		$this->Profile->User->Story->unbindModel(array('hasMany' => array('Image')));
		$stories = $this->Profile->User->Story->find('all', array('order' => 'RAND()', 'limit' => 20,  'conditions' => array('Story.user_id' => $profile['Profile']['user_id'])));
		
		$this->set('stories', $stories);
		
		/* the code below is a nasty hack to get the places ive been working */
		/* added by Bran Geffon */
		$this->Visited->recursive = 1;
		$visited = $this->Visited->find('all', array('conditions' => array('Visited.profile_id' => $profile['Profile']['id']), 'order' => 'RAND()', 'limit' => 20));
		foreach($visited as &$visit)
		{
			$this->Profile->City->recursive = -1;
			$city = $this->Profile->City->findById($visit['Visited']['city_id']);
			foreach($city['City'] as $key => $val)
			 $visit['City']["$key"] = $val;
		}
		$this->set('visited',$visited);

		unset($id);
	}

	function edit() {
		/* this action automatically display the profile for the user that's logged in */
		/* determine id of logged in user, so that we can fetch profile information */
		$user = $this->Auth->user();
		$user_id = $user['User']['id'];
		$profile = $this->Profile->findByUserId($user_id);
				
		
		if (!empty($this->data)) {
			
			/* clean all html tags out, ie < -> &lt;, > -> &rt; */
			foreach($this->data['Profile'] as $key => &$val)
			{
				$patterns = array( "/</", "/>/",);
			    $replacements = array( "&lt;", "&gt;");
				$val = preg_replace($patterns, $replacements, $val);
			}
			
			if(strlen($this->data['Profile']['about_me']) > 10000)
		     $this->data['Profile']['about_me'] = substr($this->data['Profile']['about_me'],0,10000) . '...truncated due to length.';
		     
			
			
			
			//$this->data['Profile']['user_id'] = $user_id;
			$this->Profile->id = $profile['Profile']['id'];
			
			if(isset($this->data['City']))
			{
				$this->data['Profile']['city_id'] = $this->data['City']['id'];
				unset($this->data['City']);
			}
			
			if ($this->Profile->save($this->data)) {
				$this->Session->setFlash(__('The profile has been saved.', true));
				$this->redirect(array('action'=>'view'));
			} else {
				$this->Session->setFlash(__('Could not save profile. Try again.', true));
			}
		}
		
		/* fetch profile by the user_id */
		if (empty($this->data)) {
			$this->data = $profile;
			/* change back < from &lt, and > from &rt; */
			if($this->data['Profile']['id'])
			{
				foreach($this->data['Profile'] as $key => &$val)
				{
					$replacements = array( "<", ">",);
				    $patterns = array( "/&lt;/", "/&gt;/");
					$val = preg_replace($patterns, $replacements, $val);
				}
			}
		}
	
		
	}

}
?>